<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .w {
            height: 200px;
            width: 1200px;
            line-height: 200px;
            margin: 0 auto;
            text-align: center;
            margin-bottom: 10px;
        }
        .header {
            background-color: turquoise;
        }
        .banner {
            background-color: violet;
        }
        .main {
            background-color: yellow;
            height: 800px;
        }
        .slider-bar {
            position: absolute;
            top: 200px;
            right: 100px;
            width: 50px;
            height: 150px;
            background-color: tomato;
        }
        .goBack {
            display: none;
            position: absolute;
            bottom: 0;

        }
    </style>
</head>
<body>
    <div class="slider-bar">
        <span class="goBack">返回顶部</span>
    </div>
    <div class="header w">头部</div>
    <div class="banner w">版心</div>
    <div class="main w" >主体</div>
    <script>
        var sliderbar = document.querySelector('.slider-bar');
        var banner = document.querySelector('.banner');
        // console.log(banner.offsetTop);被卷去的部分
        var bannerTop = banner.offsetTop;
        //这是整个页面文档加时间监听，而不只是sliderbar
        document.addEventListener('scroll',function(){
            //页面被卷去的头部
            console.log(window.pageYOffset);
            if(window.pageYOffset>=banner.offsetTop){
                sliderbar.style.position = 'fixed';
            }else {
                sliderbar.style.position = 'absolute';
            }
        })
    </script>
</body>
</html>